//package com.example.xhandroidexample.windows.floatwindow;
//
//import android.animation.ValueAnimator;
//import android.app.Activity;
//import android.graphics.PixelFormat;
//import android.os.Build;
//import android.util.Log;
//import android.view.Gravity;
//import android.view.LayoutInflater;
//import android.view.MotionEvent;
//import android.view.SurfaceHolder;
//import android.view.SurfaceView;
//import android.view.View;
//import android.view.ViewGroup;
//import android.view.WindowManager;
//
//import com.example.xhandroidexample.R;
//
//import static android.content.Context.WINDOW_SERVICE;
//
//public class FloatWindowHelper {
//
//    private static final String TAG = "FloatWindowHelper";
//    private WindowManager windowManager;
//    private WindowManager.LayoutParams wmParams;
//    private SurfaceHolder holder;
//    private SurfaceView sfv;
//    private PlayerHelper playerHelper;
//    private ViewGroup inflate;
//    private ValueAnimator valueAnimator;
//
//    public FloatWindowHelper() {
//        playerHelper = new PlayerHelper();
//    }
//
//    public void showFloatWindow(Activity activity) {
//
//        windowManager = (WindowManager) activity.getSystemService(WINDOW_SERVICE);
//        wmParams = new WindowManager.LayoutParams();
//        wmParams.packageName = activity.getPackageName();
//        wmParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
//                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
//                | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
//                | WindowManager.LayoutParams.FLAG_SCALED
//                | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
//                | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
//
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
//            wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
//        } else {
//            wmParams.type = WindowManager.LayoutParams.TYPE_PHONE;
//        }
//
////        if (float_window_type == FLOAT_WINDOW_TYPE_DIALOG) {
////            //wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
////            wmParams.type = WindowManager.LayoutParams.TYPE_TOAST;
////        } else if (float_window_type == FLOAT_WINDOW_TYPE_ALERT_WINDOW) {
////            //需要权限
////            wmParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
////        }
//
//        //比当前窗口层级小  出不去  1   切出去 再切进来不消失
////        wmParams.type = WindowManager.LayoutParams.FIRST_APPLICATION_WINDOW;
//        //也是出不去  2    切出去 再切进来不消失
////        wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION;
//
//        //3   也是出不去   切出去 再切进来消失
////        wmParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_STARTING;
//        //4  也是出不去 切出去 再切进来不消失
////        wmParams.type = WindowManager.LayoutParams.TYPE_DRAWN_APPLICATION;
//        //99  也是出不去 切出去 再切进来不消失
////        wmParams.type = WindowManager.LayoutParams.LAST_APPLICATION_WINDOW;
//        //1000  也是出不去 切出去 再切进来不消失
////        wmParams.type = WindowManager.LayoutParams.FIRST_SUB_WINDOW;
////        wmParams.type = WindowManager.LayoutParams.LAST_SUB_WINDOW;
//        wmParams.format = PixelFormat.RGBA_8888;
//        wmParams.gravity = Gravity.CENTER;
//
//        wmParams.width = 800;
//        wmParams.height = 800;
//
//        inflate = (ViewGroup) LayoutInflater.from(activity).inflate(R.layout.float_content, null);
////        ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
//
//        initSurfaceView(activity);
//
//        inflate.setOnTouchListener(new View.OnTouchListener() {
//            private int x;
//            private int y;
//
//            @Override
//            public boolean onTouch(View view, MotionEvent event) {
//                switch (event.getAction()) {
//                    case MotionEvent.ACTION_DOWN:
//                        if (valueAnimator != null && valueAnimator.isRunning()) {
//                            valueAnimator.cancel();
//                        }
//                        x = (int) event.getRawX();
//                        y = (int) event.getRawY();
//                        break;
//                    case MotionEvent.ACTION_MOVE:
//                        int nowX = (int) event.getRawX();
//                        int nowY = (int) event.getRawY();
//                        int movedX = nowX - x;
//                        int movedY = nowY - y;
//                        x = nowX;
//                        y = nowY;
//                        wmParams.x = wmParams.x + movedX;
//                        wmParams.y = wmParams.y + movedY;
//
//                        // 更新悬浮窗控件布局
//                        windowManager.updateViewLayout(view, wmParams);
//                        break;
//                    case MotionEvent.ACTION_UP:
//                        checkBound(activity);
//                        break;
//                    default:
//                        break;
//                }
//                return false;
//            }
//        });
//
//
//        windowManager.addView(inflate, wmParams);
//    }
//
//    private void checkBound(Activity activity) {
//        int screenWidth = activity.getResources().getDisplayMetrics().widthPixels;
//        int floatWindwoWidth = wmParams.width;
//        int dx = wmParams.x;
//        int limit = screenWidth / 2 - floatWindwoWidth / 2;
//        if (dx < 0) {
//            if (limit + dx < 0) {
//                //还原回来
//                resume(-limit);
//            }
//        } else {
//            if (limit - dx < 0) {
//                //还原回来
//                resume(limit);
//            }
//        }
//
//
//    }
//
//    private void resume(int maxDx) {
//        valueAnimator = ValueAnimator.ofInt(wmParams.x,maxDx);
//        valueAnimator.setDuration(1000);
//        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//            @Override
//            public void onAnimationUpdate(ValueAnimator animation) {
//                int value = (int) animation.getAnimatedValue();
//                wmParams.x = value;
//                // 更新悬浮窗控件布局
//                windowManager.updateViewLayout(inflate, wmParams);
//            }
//        });
//        valueAnimator.start();
//    }
//
//    private void initSurfaceView(Activity activity) {
//        sfv = inflate.findViewById(R.id.sfv);
//        holder = sfv.getHolder();
//        holder.setKeepScreenOn(true);
//        holder.addCallback(new SurfaceHolder.Callback() {
//            @Override
//            public void surfaceCreated(SurfaceHolder holder) {
//                playerHelper.init(activity, holder, sfv);
//                playerHelper.readyPlay();
//            }
//
//            @Override
//            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
//                Log.d(TAG, "surfaceChanged: width = " + width + " height = " + height);
//            }
//
//            @Override
//            public void surfaceDestroyed(SurfaceHolder holder) {
//
//            }
//        });
//    }
//
//    protected void onDestroy() {
//        playerHelper.onDestroy();
//    }
//}
